/**
 * Copyright (C) 2010 MOBICOM. All rights reserved.
 * 
 * 
 * This software is the confidential and proprietary information of
 * Mobicom or one of its subsidiaries. You shall not disclose this
 * confidential information and shall use it only in accordance with
 * the terms of the license agreement or other applicable agreement
 * you entered into with Mobicom.
 * 
 * MOBICOM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. MOBICOM
 * SHALL NOT BE LIABLE FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 * ITS DERIVATIVES.
 * 
 * Apr 13, 2011 4:27:45 AM
 * 
 * @author Nguyen Dinh Phuoc Duc(ndpduc@gmail.com)
 */
package com.lily.smsbox.helper;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;

import com.lily.smsbox.model.Contact;

/**
 * Internal Class
 * 
 * @author Nguyen Dinh Phuoc Duc (ndpduc@gmail.com)
 * 
 */
public class ContactHelper {
  private Context _context;

  public ContactHelper(Context context) {
    _context = context;
  }

  public Contact getContactById(long id) {
    ContentResolver cr = _context.getContentResolver();
    Uri uri =
        Uri.withAppendedPath(People.CONTENT_URI, String.valueOf(id));
    Cursor c = cr.query(uri, null, null, null, null);
    Contact contact = null;
    if (c.moveToFirst()) {
      contact = new Contact();
      contact.displayName =
          c.getString(c.getColumnIndex(People.DISPLAY_NAME));
    }
    return contact;
  }

  public Contact getContactByKey(String key) {
    ContentResolver cr = _context.getContentResolver();
    Uri uri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, key);
    Cursor c = cr.query(uri, null, null, null, null);
    Contact contact = null;
    if (c.moveToFirst()) {
      contact = new Contact();
      contact.displayName =
          c.getString(c.getColumnIndex(Contacts.Phones.DISPLAY_NAME));
      contact.id = c.getLong(c.getColumnIndex(Phones.PERSON_ID));
    }
    return contact;
  }

}
